package com.shopcart.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopcart.controller.CustomerController;
import com.shopcart.entity.Customer;

@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ProductServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Long productId = Long.valueOf(request.getParameter("productId"));
		HttpSession session = request.getSession();

		Customer customer = (Customer) session.getAttribute("customer");

		CustomerController customerController = null;
		Context ctx = null;
		try {
			ctx = new InitialContext();
			customerController = (CustomerController) ctx.lookup("ejb/CustomerControllerBean");
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
		customer = customerController.deleteNotExistingProduct(customer, productId);
		session.setAttribute("customer", customer);
	}
}